using Amazon.ECS.Model;
using System;
namespace RomanJendrusz.Amazon.ETL.Messages.MessageMappers
{
    public class GetBookOffersCountMapper: MessageMapper<GetBookOffersCountRequest, ItemLookupRequest, ItemLookupResponse>
    {
        public override AmazonResponse MapToAmazonResponse(ItemLookupResponse awsResponse)
        {
            var item = GetRequestItem(awsResponse, 0);
            if(item.IsSetOffers() == false)
            {
                throw new InvalidOperationException("The message does not contain offers informations");
            }
            return new GetItemsCountResponse(CorrelationId)
                       {
                           ItemsCount = Convert.ToInt32(item.Offers.TotalOffers),
                           RowsPerPage = Convert.ToInt32(item.Offers.Offer.Count)
                       };
        }

        public override ItemLookupRequest MapToAwsRequest(GetBookOffersCountRequest request)
        {
            var awsRequest = new ItemLookupRequest { MerchantId = request.MerchantId };
            awsRequest.ItemId.Add(request.BookAsin);
            //awsRequest.IdType = "ISBN";
            //awsRequest.SearchIndex = "Books";
            switch(request.Condition)
            {
                case OffersCondition.All:
                    awsRequest.Condition = Condition.All;
                    break;
                case OffersCondition.Collectible:
                    awsRequest.Condition = Condition.Collectible;
                    break;
                case OffersCondition.New:
                    awsRequest.Condition = Condition.New;
                    break;
                case OffersCondition.Refurbished:
                    awsRequest.Condition = Condition.Refurbished;
                    break;
                case OffersCondition.Used:
                    awsRequest.Condition = Condition.Used;
                    break;
            }
            awsRequest.ResponseGroup.Add(request.ResponseGroup);
            awsRequest.MerchantId = request.MerchantId;
            return awsRequest;
        }
    }
}